namespace CIS.Customer
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class CustomerInfoStudyPage : Page
    {
        protected DropDownList DegreeDdl;
        protected CheckBox EditChk;
        protected DropDownList FromMonthDdl;
        protected DropDownList FromYearDdl;
        protected TextBox GranduateSchoolTxb;
        protected Image iBtnCustomer;
        protected ImageButton iBtnGray5;
        protected ImageButton iBtnOrange0;
        protected ImageButton iBtnOrange1;
        protected ImageButton iBtnOrange2;
        protected ImageButton iBtnOrange3;
        protected ImageButton iBtnOrange4;
        protected ImageButton iBtnOrange6;
        protected ImageButton iBtnOrange7;
        protected ImageButton iBtnOrange8;
        protected Label lbDegree;
        protected Label lbEdit;
        protected Label lbFrom;
        protected Label lbFromM;
        protected Label lbFromY;
        protected Label lbGranduateSchool;
        protected Label lbMajor;
        protected Label lbSaveMsg;
        protected Label lbSponsor;
        protected Label lbTo;
        protected Label lbToM;
        protected Label lbToY;
        protected TextBox MajorTxb;
        protected UserPrincipal Principal = null;
        protected Repeater rptStudy;
        protected Button SaveBtn;
        protected TextBox SponsorTxb;
        protected DropDownList ToMonthDdl;
        protected DropDownList ToYearDdl;

        protected string AddStrPrefix(object obj, string strPre)
        {
            if (obj.ToString() == string.Empty)
            {
                return "";
            }
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/LanguageConfig.xml");
            return (ManageSystemConfig.GetSystemSetLanguage("CustomerInfoStudyPage", strPre, language, path) + obj.ToString());
        }

        protected string DecryptString(object obj)
        {
            return ManageOther.DecodeWithBytes(obj.ToString());
        }

        private void FillPage()
        {
            Guid customerId = new Guid(base.Request.QueryString["CustomerId"]);
            if ((this.Principal.SysUser.IsAdmin <= 0) && !ManageSystemConfig.IsThisCustomerMaster(this.Principal.SysUser.UserId, customerId))
            {
                this.MajorTxb.ReadOnly = true;
                this.GranduateSchoolTxb.ReadOnly = true;
                this.SponsorTxb.ReadOnly = true;
                this.SaveBtn.Visible = false;
                this.lbEdit.Visible = false;
                this.EditChk.Visible = false;
                this.SaveBtn.Visible = false;
            }
            this.RptDataBind();
        }

        private void iBtnOrange0_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/Customer/CustomerBasicInfoPage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange1_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/Customer/CustomerInfoPersonalPage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange2_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/Customer/CustomerInfoPreferencePage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange3_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/Customer/CustomerInfoHealthCostumePage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange4_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/Customer/CustomerInfoTravelPage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange6_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/Customer/CustomerInfoWorkPage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange7_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/Customer/CustomerInfoFamilyPage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange8_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/Customer/CustomerInfoOtherPage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void InitializeComponent()
        {
            this.iBtnOrange0.Click += new ImageClickEventHandler(this.iBtnOrange0_Click);
            this.iBtnOrange1.Click += new ImageClickEventHandler(this.iBtnOrange1_Click);
            this.iBtnOrange2.Click += new ImageClickEventHandler(this.iBtnOrange2_Click);
            this.iBtnOrange3.Click += new ImageClickEventHandler(this.iBtnOrange3_Click);
            this.iBtnOrange4.Click += new ImageClickEventHandler(this.iBtnOrange4_Click);
            this.iBtnOrange6.Click += new ImageClickEventHandler(this.iBtnOrange6_Click);
            this.iBtnOrange7.Click += new ImageClickEventHandler(this.iBtnOrange7_Click);
            this.iBtnOrange8.Click += new ImageClickEventHandler(this.iBtnOrange8_Click);
            this.rptStudy.ItemCommand += new RepeaterCommandEventHandler(this.rptStudy_ItemCommand);
            this.SaveBtn.Click += new EventHandler(this.SaveBtn_Click);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFromImage();
            this.SetFrom();
            this.DegreeDdl.Items.Clear();
            ListItem tmpItem11 = new ListItem();
            ListItem tmpItem22 = new ListItem();
            ListItem tmpItem33 = new ListItem();
            ListItem tmpItem44 = new ListItem();
            ListItem tmpItem55 = new ListItem();
            ListItem tmpItem66 = new ListItem();
            ListItem tmpItem77 = new ListItem();
            ListItem tmpItem88 = new ListItem();
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    tmpItem11.Text = "大专以下";
                    tmpItem22.Text = "大专";
                    tmpItem33.Text = "本科";
                    tmpItem44.Text = "硕士";
                    tmpItem55.Text = "博士";
                    tmpItem66.Text = "MBA";
                    tmpItem77.Text = "EMBA";
                    tmpItem88.Text = "DBA";
                    tmpItem11.Value = "大专以下";
                    tmpItem22.Value = "大专";
                    tmpItem33.Value = "本科";
                    tmpItem44.Value = "硕士";
                    tmpItem55.Value = "博士";
                    tmpItem66.Value = "MBA";
                    tmpItem77.Value = "EMBA";
                    tmpItem88.Value = "DBA";
                    break;

                case "en":
                    tmpItem11.Text = "Below";
                    tmpItem22.Text = "College";
                    tmpItem33.Text = "Bachelor";
                    tmpItem44.Text = "Master";
                    tmpItem55.Text = "doctor";
                    tmpItem66.Text = "MBA";
                    tmpItem77.Text = "EMBA";
                    tmpItem88.Text = "DBA";
                    tmpItem11.Value = "大专以下";
                    tmpItem22.Value = "大专";
                    tmpItem33.Value = "本科";
                    tmpItem44.Value = "硕士";
                    tmpItem55.Value = "博士";
                    tmpItem66.Value = "MBA";
                    tmpItem77.Value = "EMBA";
                    tmpItem88.Value = "DBA";
                    break;
            }
            this.DegreeDdl.Items.Add(tmpItem11);
            this.DegreeDdl.Items.Add(tmpItem22);
            this.DegreeDdl.Items.Add(tmpItem33);
            this.DegreeDdl.Items.Add(tmpItem44);
            this.DegreeDdl.Items.Add(tmpItem55);
            this.DegreeDdl.Items.Add(tmpItem66);
            this.DegreeDdl.Items.Add(tmpItem77);
            this.DegreeDdl.Items.Add(tmpItem88);
            this.lbSaveMsg.Text = string.Empty;
            this.FromYearDdl.Items.Clear();
            this.FromMonthDdl.Items.Clear();
            ListItem tmpItem = new ListItem();
            tmpItem.Text = "----";
            this.FromYearDdl.Items.Add(tmpItem);
            ListItem tmpItem1 = new ListItem();
            tmpItem1.Text = "--";
            this.FromMonthDdl.Items.Add(tmpItem1);
            for (int i = DateTime.Now.Year; i >= 0x78a; i--)
            {
                ListItem YearItem = new ListItem();
                YearItem.Text = i.ToString();
                this.FromYearDdl.Items.Add(YearItem);
            }
            for (int i = 1; i <= 12; i++)
            {
                ListItem MonthItem = new ListItem();
                MonthItem.Text = i.ToString();
                this.FromMonthDdl.Items.Add(MonthItem);
            }
            this.ToYearDdl.Items.Clear();
            this.ToMonthDdl.Items.Clear();
            ListItem tmpItem2 = new ListItem();
            tmpItem2.Text = "----";
            this.ToYearDdl.Items.Add(tmpItem);
            ListItem tmpItem3 = new ListItem();
            tmpItem3.Text = "--";
            this.ToMonthDdl.Items.Add(tmpItem1);
            for (int i = DateTime.Now.Year; i >= 0x78a; i--)
            {
                ListItem YearItem = new ListItem();
                YearItem.Text = i.ToString();
                this.ToYearDdl.Items.Add(YearItem);
            }
            for (int i = 1; i <= 12; i++)
            {
                ListItem MonthItem = new ListItem();
                MonthItem.Text = i.ToString();
                this.ToMonthDdl.Items.Add(MonthItem);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.SelectAuth();
            }
        }

        private void RptDataBind()
        {
            this.rptStudy.DataSource = ManageCustomer.GetCustomerStudyExperienceInfo(new Guid(base.Request.QueryString["CustomerId"]));
            this.rptStudy.DataBind();
        }

        private void rptStudy_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (this.Principal.OwnRole.TypeB == 0)
            {
                base.Response.Write("<script> alert('没有权限操作！') </script>");
            }
            else
            {
                Guid customerId = new Guid(base.Request.QueryString["CustomerId"]);
                if ((this.Principal.SysUser.IsAdmin <= 0) && !ManageSystemConfig.IsThisCustomerMaster(this.Principal.SysUser.UserId, customerId))
                {
                    base.Response.Write("<script> alert('没有权限操作！') </script>");
                }
                else
                {
                    string cmd = e.CommandName;
                    string strTmp = (string) e.CommandArgument;
                    Guid customerStudyExpId = new Guid(strTmp);
                    switch (cmd)
                    {
                        case "delete":
                            ManageCustomer.RemoveCustomerStudyExperienceInfoViaId(customerStudyExpId);
                            this.InitPage();
                            this.FillPage();
                            break;

                        case "modify":
                        {
                            CustomerStudyExperience customerStudyExperience = ManageCustomer.GetCustomerStudyExperienceInfoViaId(customerStudyExpId);
                            if (customerStudyExperience != null)
                            {
                                try
                                {
                                    this.MajorTxb.Text = customerStudyExperience.Major;
                                    this.GranduateSchoolTxb.Text = customerStudyExperience.GranduateSchool;
                                    this.SponsorTxb.Text = customerStudyExperience.Sponsor;
                                    for (int i = 0; i < this.DegreeDdl.Items.Count; i++)
                                    {
                                        if (this.DegreeDdl.Items[i].Value == customerStudyExperience.Degree)
                                        {
                                            this.DegreeDdl.SelectedIndex = i;
                                            break;
                                        }
                                    }
                                    if ((customerStudyExperience.StartDate.Year == 0x708) && (customerStudyExperience.StartDate.Month == 1))
                                    {
                                        this.FromYearDdl.SelectedIndex = 0;
                                        this.FromMonthDdl.SelectedIndex = 0;
                                    }
                                    else
                                    {
                                        for (int i = 0; i < this.FromYearDdl.Items.Count; i++)
                                        {
                                            if (this.FromYearDdl.Items[i].Text == customerStudyExperience.StartDate.Year.ToString())
                                            {
                                                this.FromYearDdl.SelectedIndex = i;
                                                break;
                                            }
                                        }
                                        for (int i = 0; i < this.FromMonthDdl.Items.Count; i++)
                                        {
                                            if (this.FromMonthDdl.Items[i].Text == customerStudyExperience.StartDate.Month.ToString())
                                            {
                                                this.FromMonthDdl.SelectedIndex = i;
                                                break;
                                            }
                                        }
                                    }
                                    if ((customerStudyExperience.EndDate.Year == 0x708) && (customerStudyExperience.EndDate.Month == 1))
                                    {
                                        this.ToYearDdl.SelectedIndex = 0;
                                        this.ToMonthDdl.SelectedIndex = 0;
                                    }
                                    else
                                    {
                                        for (int i = 0; i < this.ToYearDdl.Items.Count; i++)
                                        {
                                            if (this.ToYearDdl.Items[i].Text == customerStudyExperience.EndDate.Year.ToString())
                                            {
                                                this.ToYearDdl.SelectedIndex = i;
                                                break;
                                            }
                                        }
                                        for (int i = 0; i < this.ToMonthDdl.Items.Count; i++)
                                        {
                                            if (this.ToMonthDdl.Items[i].Text == customerStudyExperience.EndDate.Month.ToString())
                                            {
                                                this.ToMonthDdl.SelectedIndex = i;
                                                break;
                                            }
                                        }
                                    }
                                    this.EditChk.Checked = true;
                                    this.Page.Session.Add("CustomerStudyExpId", customerStudyExperience.CustomerStudyExpId.ToString());
                                }
                                catch (Exception)
                                {
                                }
                            }
                            break;
                        }
                    }
                }
            }
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            string strDegree = this.DegreeDdl.SelectedItem.Value;
            string strMajor = this.MajorTxb.Text.Trim();
            string strGranduateSchool = this.GranduateSchoolTxb.Text.Trim();
            string strSponsor = this.SponsorTxb.Text.Trim();
            try
            {
                DateTime startDate = new DateTime(0x708, 1, 1);
                DateTime endDate = new DateTime(0x708, 1, 1);
                if ((this.FromYearDdl.SelectedItem.Text != "----") || (this.FromMonthDdl.SelectedItem.Text != "--"))
                {
                    startDate = new DateTime(Convert.ToInt32(this.FromYearDdl.SelectedItem.Text), Convert.ToInt32(this.FromMonthDdl.SelectedItem.Text), 1);
                }
                if ((this.ToYearDdl.SelectedItem.Text != "----") || (this.ToMonthDdl.SelectedItem.Text != "--"))
                {
                    endDate = new DateTime(Convert.ToInt32(this.ToYearDdl.SelectedItem.Text), Convert.ToInt32(this.ToMonthDdl.SelectedItem.Text), 1);
                }
                if (this.EditChk.Checked)
                {
                    ManageCustomer.UpdateCustomerStudyExperienceInfo(new Guid(this.Page.Session["CustomerStudyExpId"].ToString()), strDegree, strMajor, strGranduateSchool, strSponsor, startDate, endDate);
                    ManageLog.InsertLog(this.Principal.SysUser.UserId, this.Principal.SysUser.LoginName, DateTime.Now, "UpdateStudy", "IP:" + base.Request.UserHostAddress + " DNS:" + base.Request.UserHostName, ManageCustomer.GetCustomerBasicInfoViaId(new Guid(base.Request.QueryString["CustomerId"])).Name, new Guid(base.Request.QueryString["CustomerId"]));
                }
                else
                {
                    ManageCustomer.InsertNewCustomerStudyExperienceInfo(new Guid(base.Request.QueryString["CustomerId"]), strDegree, strMajor, strGranduateSchool, strSponsor, startDate, endDate);
                    ManageLog.InsertLog(this.Principal.SysUser.UserId, this.Principal.SysUser.LoginName, DateTime.Now, "InsertStudy", "IP:" + base.Request.UserHostAddress + " DNS:" + base.Request.UserHostName, ManageCustomer.GetCustomerBasicInfoViaId(new Guid(base.Request.QueryString["CustomerId"])).Name);
                }
                this.EditChk.Checked = false;
                this.DegreeDdl.SelectedIndex = 0;
                this.MajorTxb.Text = string.Empty;
                this.GranduateSchoolTxb.Text = string.Empty;
                this.SponsorTxb.Text = string.Empty;
                this.InitPage();
            }
            catch (ArgumentOutOfRangeException exOutOfRange)
            {
                this.lbSaveMsg.Text = exOutOfRange.Message;
                return;
            }
            catch (Exception ex)
            {
                this.lbSaveMsg.Text = ex.Message;
                return;
            }
            this.RptDataBind();
            this.lbSaveMsg.Text = "Save successfully!";
        }

        private void SelectAuth()
        {
            if (this.Principal.OwnRole.TypeB == 0)
            {
                this.InitPage();
                this.FillPage();
                this.MajorTxb.ReadOnly = true;
                this.GranduateSchoolTxb.ReadOnly = true;
                this.SponsorTxb.ReadOnly = true;
                this.SaveBtn.Visible = false;
                this.lbEdit.Visible = false;
                this.EditChk.Visible = false;
                this.SaveBtn.Visible = false;
            }
            else if (this.Principal.OwnRole.TypeB == 1)
            {
                this.InitPage();
                this.FillPage();
            }
        }

        protected string SetDegreeStr(object obj)
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    return obj.ToString();
            }
            switch (((string) obj))
            {
                case "大专以下":
                    return "Below";

                case "大专":
                    return "College";

                case "本科":
                    return "Bachelor";

                case "硕士":
                    return "Master";

                case "博士":
                    return "doctor";

                case "MBA":
                    return "MBA";

                case "EMBA":
                    return "EMBA";

                case "DBA":
                    return "DBA";
            }
            return "";
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/LanguageConfig.xml");
            this.lbFrom.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoStudyPage", "lbFrom", language, path);
            this.lbFromY.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoStudyPage", "lbFromY", language, path);
            this.lbFromM.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoStudyPage", "lbFromM", language, path);
            this.lbTo.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoStudyPage", "lbTo", language, path);
            this.lbToY.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoStudyPage", "lbToY", language, path);
            this.lbToM.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoStudyPage", "lbToM", language, path);
            this.lbDegree.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoStudyPage", "lbDegree", language, path);
            this.lbMajor.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoStudyPage", "lbMajor", language, path);
            this.lbGranduateSchool.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoStudyPage", "lbGranduateSchool", language, path);
            this.lbSponsor.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoStudyPage", "lbSponsor", language, path);
            this.lbEdit.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoStudyPage", "lbEdit", language, path);
            this.SaveBtn.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoStudyPage", "SaveBtn", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iBtnCustomer.ImageUrl = "../images/title_customerinfo.jpg";
                    this.iBtnOrange0.ImageUrl = "../images/title_CustomerInfo_lable_1_orange.jpg";
                    this.iBtnOrange1.ImageUrl = "../images/title_CustomerInfo_lable_2_orange.jpg";
                    this.iBtnOrange2.ImageUrl = "../images/title_CustomerInfo_lable_3_orange.jpg";
                    this.iBtnOrange3.ImageUrl = "../images/title_CustomerInfo_lable_4_orange.jpg";
                    this.iBtnOrange4.ImageUrl = "../images/title_CustomerInfo_lable_5_orange.jpg";
                    this.iBtnGray5.ImageUrl = "../images/title_CustomerInfo_lable_6_Gray.jpg";
                    this.iBtnOrange6.ImageUrl = "../images/title_CustomerInfo_lable_7_orange.jpg";
                    this.iBtnOrange7.ImageUrl = "../images/title_CustomerInfo_lable_8_orange.jpg";
                    this.iBtnOrange8.ImageUrl = "../images/title_CustomerInfo_lable_9_orange.gif";
                    break;

                case "en":
                    this.iBtnCustomer.ImageUrl = "../images/title_customerinfoEn.jpg";
                    this.iBtnOrange0.ImageUrl = "../images/title_CustomerInfoEn_lable_1_orange.jpg";
                    this.iBtnOrange1.ImageUrl = "../images/title_CustomerInfoEn_lable_2_orange.jpg";
                    this.iBtnOrange2.ImageUrl = "../images/title_CustomerInfoEn_lable_3_orange.jpg";
                    this.iBtnOrange3.ImageUrl = "../images/title_CustomerInfoEn_lable_4_orange.jpg";
                    this.iBtnOrange4.ImageUrl = "../images/title_CustomerInfoEn_lable_5_orange.jpg";
                    this.iBtnGray5.ImageUrl = "../images/title_CustomerInfoEn_lable_6_Gray.jpg";
                    this.iBtnOrange6.ImageUrl = "../images/title_CustomerInfoEn_lable_7_orange.jpg";
                    this.iBtnOrange7.ImageUrl = "../images/title_CustomerInfoEn_lable_8_orange.jpg";
                    this.iBtnOrange8.ImageUrl = "../images/title_CustomerInfoEn_lable_9_orange.gif";
                    break;
            }
        }

        protected string ValidateDateTime(object tmpObj)
        {
            string strTmp = tmpObj.ToString();
            if (strTmp.Substring(0, 4) == "1800")
            {
                return "---- -- ";
            }
            return strTmp;
        }
    }
}

